/*
* BankApplication
*
* r4
*
* GNU General Public License v3.0
*/

package com.acme.bankapp;

import java.io.*;

import com.acme.bankapp.domain.bank.*;
import com.acme.bankapp.service.bank.*;
import com.acme.bankapp.service.email.EmailService;
import com.acme.net.BankServer;

public class BankApplication {

	private static String feedFile = "";
	private static boolean server = false;
	
	public static EmailService emailService = new EmailService();
	
	enum ParamType {
		NONE, LOAD_BANK
	}
	
	public static void parseArgs(String[] args) {
		ParamType paramType = ParamType.NONE;
		
		for(String arg : args) {
			if(paramType == ParamType.LOAD_BANK) {
				feedFile = arg;
			}
			if(arg.equals("-loadbank")) {
				paramType = ParamType.LOAD_BANK;
			} else if(arg.equals("-server")) {
				server = true;
				paramType = ParamType.NONE;
			} else {
				paramType = ParamType.NONE;
			}
		}
	}
	
	/*
	 * Run like: BankApplication -loadbank C:\feed.txt [-server]
	 */
	public static void main(String[] args) {
		Bank bank = new Bank();
		BankService bankService = new BankService(bank);
		BankServer bankServer = new BankServer();
		
		parseArgs(args);
		
		try {
			bankService.setFeedFile(feedFile);
			bankService.readBank();
		} catch(FileNotFoundException ex) {
			System.out.println("Feed file not found");
		}
		
		System.out.println(bankService);
		
		if(server) {
			bankServer.startServer(bankService);
		}
		
		emailService.close();
	}

}
